Not all command generate an error, some may issue warnings.
Reading errors
Errors are not failures. They are information.
When you get an error:
read the first line carefully
identify the object or function mentioned
check spelling and capitalization
check whether the package is loaded
run the code line by line
Exercice 4
Vectors
A vector stores several values of the same basic type.
In R, you can specify vectors inside of the c()
Each element of the vector is separated by a coma.
communes <-c("Carouge", "Cologny", "Lancy", "Meyrin")population <-c(22600, 5700, 34400, 27000)# You can then perform operations on these vectorsaverage_population <-mean(population)average_population
[1] 22425
Vectors
Vectors are unidimensional (VS data frame or matrix).
You can access content of vectors by specifying the index of the observation
communes[2] # Get the second element of the vector
commune population
1 Carouge 22600
2 Cologny 5700
3 Lancy 34400
4 Meyrin 27000
Data frames
Data frames have two dimensions, rows and columns.
You can access the content of specific cells by specifying the index for the rows, and the columns
data[row,column]
municipal_data[1,] ## First row all columns
commune population
1 Carouge 22600
municipal_data[,1] ## First column all rows
[1] "Carouge" "Cologny" "Lancy" "Meyrin"
municipal_data[1,1] ## First row and first column
[1] "Carouge"
Data frames
In data frames, the columns are the variables, the lines are the observations.
You can also access the content of specific variable using the atomic character $
municipal_data$population
[1] 22600 5700 34400 27000
Exercice 6
Functions
A function takes inputs, performs operations, and returns an output.
To create a function, you have a built in function option.
divide_by_4_add_1 <-function(x) { x /4+1}divide_by_4_add_1(20)
[1] 6
Functions
You can also have multiple arguments in a function
divide_by_4_add_n <-function(x, n) { x /4+ n}divide_by_4_add_n(20, 4)
[1] 9
Functions
Functions can also have default values
divide_by_4_add_n <-function(x =10, n =4) { x /4+ n}divide_by_4_add_n(20, 2) # Same as before
[1] 7
divide_by_4_add_n() # Function with default value
[1] 6.5
Functions
Finally functions can also be more or less robust and well documented
If I run: divide_by_4_add_n("Cat", "Dog")Error in x/4 : non-numeric argument to binary operator
divide_by_4_add_n <-function(x, n) {if (!is.numeric(x) |!is.numeric(n)) {stop(paste0("x and n must be numeric, but an object of class non-numeric was provided. \n x is '",class(x)[1], "'\n and n is '",class(n)[1], "'" ) ) } x /4+ n}
Exercice 7
Packages and libraries
Packages extend what R can do.
Install a package once:
install.packages("readr")
Load it in each new R session:
library(readr)
Careful: For some reason, you need to put “” when installing package, but not when loading libraries.
Packages and libraries
There are lots of packages and libraries functionning in R.
You can also developp your own package and make it available for everyone.
Some packages are available not with the official distribution channel. You can use function from the devtools or the remotes packages to install these softwares.
Exercice 8
Wrap-up
Today you learned how to:
navigate the course organization and evaluation
form groups and choose a provisional project topic
distinguish R from RStudio
use the RStudio interface
write reproducible R code in a script
create objects, vectors, factors, functions, and data frames